<template>
  <view class="page">
    <!-- #ifndef MP-WEIXIN -->
    <my-navbar leftText="常用" @leftClick="back"></my-navbar>
    <!-- #endif -->
    <view class="search_box">
      <view class="search_content" @click="show = true">
        <icon type="search" size="14" />
        <view class="search_input">{{
          date.start ? `${date.start} — ${date.end}` : "最近一周"
        }}</view>
      </view>
    </view>
    <view class="goods_box">
      <view
        class="goods"
        @click="detailPage(index, item.id)"
        v-for="(item, index) in dataList"
        :key="index"
      >
        <image class="img" :src="item.img" mode="aspectFit"></image>
        <!-- <text class="name">{{ item.name }}</text>
         -->
        <view class="name_box">
          <view class="name">{{ item.name }}</view>
          <view class="tags" v-if="item.datum.length > 0">
            <view class="tag" v-for="(y, index) in item.datum" :key="index">{{
              y.title
            }}</view>
          </view>
          <!-- <view class="detail" v-else>
            <text>{{ item.describe || "" }}</text>
          </view> -->
        </view>
        <text v-show="canshowprice" class="unit"
          ><text class="price"
            >{{ item.showPrice
            }}<text
              v-if="item.spu.length == 1 && item.spu[0].current_price == 0"
              style="font-size: 24rpx"
            >
              /{{ item.spu[0].unit }}</text
            ></text
          ></text
        >
        <view class="detail">已下单：{{ item.order_number }}次</view>
        <!-- <image class="icon logo" src="../../static/image/logo.png" mode=""></image> -->
        <image
          @click.stop="showCart(index)"
          class="icon shop"
          src="/static/image/addcart.svg"
          mode=""
        ></image>
      </view>
      <view class="nodata" v-if="dataList.length == 0">
        <image :src="nodataImg" mode="aspectFit"></image>
        <view>空空如也~</view>
      </view>
    </view>
    <u-calendar
      v-model="show"
      @change="changeDate"
      mode="range"
      :change-year="false"
    ></u-calendar>
    <my-addcart
      ref="minicart"
      :canshowprice="canshowprice"
      @tabBarTag="changeTabBarTag"
    ></my-addcart>
  </view>
</template>

<script>
import { $https } from "@/static/js/request.js";
import MixinShowprice from "@/static/js/mixinshowprice.js";
export default {
  data() {
    return {
      nodataImg: getApp().globalData.nodataImg,
      dataList: [],
      show: false,
      date: {
        start: "",
        end: "",
        startTime: "", //时间戳
        endTime: "",
      },
    };
  },
  mixins: [MixinShowprice],
  onLoad() {
    let now = new Date();
    var end = (now.getTime() / 1000).toFixed(0);
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);

    var start = ((now.getTime() - 1000 * 60 * 60 * 24 * 7) / 1000).toFixed(0);
    this.date.startTime = start;
    this.date.endTime = end;
    this.getData(start, end);
  },
  methods: {
    getData(start, end) {
      let params = {
        start_time: start,
        end_time: end,
      };
      $https("GET", "commonUseList", params, (res) => {
        if (res.data.errcode == 0) {
          this.dataList = this.changePrice(res.data.data.list.list);
        } else {
          uni.showToast({
            title: res.data.errmsg,
            icon: "none",
          });
        }
      });
    },
    detailPage(index, id) {
      uni.navigateTo({
        url: `../../subindex/index/shopdetail?id=${id}`,
      });
    },
    addToCart(goodsId, spuId, num, index) {
      var options = {
        goods_id: goodsId,
        spu_id: spuId,
        num,
      };
      $https("POSTN", "changeCart", options, (res) => {
        if (res.data.errcode == 0) {
          this.dataList[index].willBuyNum = Number(num);
          this.$refs.tabar.changetag(res.data.data.all_cart_num);
        } else {
          uni.showToast({
            title: res.data.errmsg,
            icon: "none",
          });
        }
      });
    },
    changeDate(data) {
      this.date.start = data.startDate;
      this.date.end = data.endDate;
      let startTime = (new Date(`${data.startDate} 00:00:01`) / 1000).toFixed(
        0
      );
      let endTime = (new Date(`${data.endDate} 00:00:01`) / 1000).toFixed(0);
      this.date.startTime = startTime;
      this.date.endTime = endTime;
      this.getData(startTime, endTime);
    },
    //处理价格规格sku
    changePrice(data) {
      var array = JSON.parse(JSON.stringify(data));
      for (let i in array) {
        //算出价格范围
        if (array[i].spu.length > 1) {
          // var min, max;
          // min = array[i].spu[0].market_price;
          // if (array[i].spu[1].market_price < min) {
          // 	max = Number(array[i].spu[0].market_price);
          // 	min = Number(array[i].spu[1].market_price);
          // } else {
          // 	max = Number(array[i].spu[1].market_price);
          // 	min = Number(array[i].spu[0].market_price);
          // }
          // for (let y in array[i].spu) {
          // 	if (Number(array[i].spu[y].market_price) < min) {
          // 		min = Number(array[i].spu[y].market_price)
          // 	} else if (Number(array[i].spu[y].market_price) > max) {
          // 		max = Number(array[i].spu[y].market_price);
          // 	}
          // }
          // array[i].showPrice = `￥${min.toFixed(2)}~${max.toFixed(2)}`;
          let bol1 = array[i].spu.some((e) => {
            return e.current_price == 1;
          });
          let bol2 = array[i].spu.every((e) => {
            return e.current_price == 1;
          });
          let arr = [];
          array[i].spu.forEach((e) => {
            if (e.current_price != 1) {
              arr.push(e.market_price);
            }
          });
          let max = Math.max(...arr);
          let min = Math.min(...arr);
          // console.log(max,min);
          if (bol2) {
            array[i].showPrice = "时价";
          } else if (bol1) {
            array[i].showPrice = `时价~${max.toFixed(2)}`;
          } else {
            array[i].showPrice = `￥${min.toFixed(2)}~${max.toFixed(2)}`;
          }
        } else if (array[i].spu.length == 1) {
          //是时价
          if (array[i].spu[0].current_price == 1) {
            array[i].showPrice = "时价";
          } else {
            array[i].showPrice = "¥" + array[i].spu[0].market_price;
          }
          //只有一个规格时显示起订量、库存
          array[i].minimum = array[i].spu[0].minimum;
          array[i].designated_inventory = array[i].spu[0].designated_inventory;
          array[i].inventory_left = array[i].spu[0].inventory_left;
        }
        array[i].willBuyNum = array[i].num_cart_count;
      }
      return array;
    },
    //显示小购物车
    showCart(index) {
      var data = this.dataList[index];
      this.$refs.minicart.open(data);
    },
    back() {
      // #ifdef H5
      window.history.back(-1);
      // #endif
      // #ifndef H5
      uni.navigateBack();
      // #endif
    },
  },
};
</script>

<style lang="scss">
/deep/.u-drawer {
  overflow: unset !important;
}
.page {
  .search_box {
    position: fixed;
    // #ifndef MP-WEIXIN
    top: calc(80rpx + var(--status-bar-height));
    // #endif
    // #ifdef MP-WEIXIN
    top: 0;
    // #endif
    left: 0;
    right: 0;
    box-sizing: border-box;
    background: #f6f6f7;
    color: #b7b8ba;
    padding: 20rpx 50rpx;
    // margin: 20rpx auto 0;
    z-index: 10;
    .search_content {
      background: #f0f1f3;
      height: 80rpx;
      border-radius: 40rpx;
      display: flex;
      align-items: center;
      margin: auto;
      padding: 0 50rpx;
    }
    .search_input {
      margin-left: 18rpx;
      color: #999;
    }
  }
  .goods_box {
    overflow: hidden;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    width: calc(100% - 20rpx);
    margin: 130rpx auto 10rpx;
    .goods {
      float: left;
      position: relative;
      display: flex;
      flex-direction: column;
      // align-items: center;
      background: #fff;
      border-radius: 10rpx;
      width: 360rpx;
      // height:268rpx;
      box-sizing: border-box;
      padding: 12rpx 0;
      margin-bottom: 30rpx;
      .img {
        width: 360rpx;
        height: 360rpx;
        min-height: 360rpx;
      }
      .name_box {
        margin-bottom: 8rpx;
        padding: 20rpx 20rpx 0 20rpx;
        .tags {
          display: inline-flex;
          .tag {
            padding: 0 12rpx;
            background: red;
            color: #fff;
            border-radius: 6rpx;
            font-size: 24rpx;
            margin-right: 4rpx;
          }
        }
        .name {
          font-size: 26rpx;
          color: #666;
        }
      }
      .name {
        font-size: 32rpx;
        color: #191919;
        margin: 14rpx 0 10rpx;
        font-weight: bold;
      }

      .detail {
        height: 36rpx;
        font-size: 24rpx;
        padding: 0 10rpx;
        box-sizing: border-box;
        color: #ccc;
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        padding: 0 20rpx 0 20rpx;
      }
      .unit {
        margin-bottom: 8rpx;
        padding: 0 20rpx 0 20rpx;
        color: #e84b4b;
        .price {
          font-size: 26rpx;
        }
      }
      .icon {
        position: absolute;
        z-index: 1;
        bottom: 12rpx;
        &.logo {
          width: 30rpx;
          height: 34rpx;
          left: 14rpx;
        }
        &.shop {
          width: 55rpx;
          height: 55rpx;
          right: 14rpx;
        }
      }
    }
  }
  /deep/.u-calendar {
    background: #fff;
    .u-calendar__header__text {
      padding-top: 32rpx;
      margin-top: 0;
    }
  }
}
</style>
